import {Component, Input, OnInit, ViewChild} from '@angular/core';
import {ColumnType, SPage, TablePageComponent} from "@shared/components/table-page/table-page.component";
import {NzMessageService, NzModalService} from "ng-zorro-antd";
import {AreaService} from "../../sys-setting/area-manage/area.service";
import {ApiService} from "@core/services/api";
import {HttpClient} from "@angular/common/http";
import {RoomEditComponent} from "../room-edit/room-edit.component";
import {PersonListComponent} from "../person-list/person-list.component";
import {RoomDetailComponent} from "../room-detail/room-detail.component";
import {HouseManagePersonAddComponent} from "../person-add/person-add.component";
import { STData } from '@delon/abc';
import { ChangeDetectorRef } from '@angular/core';
import { ACLService } from '@delon/acl';
import { ImportPersonComponent } from '../import-person/import-person.component';
import { ImportPersonFaceComponent } from '../import-person-face/import-person-face.component';

@Component({
  selector: 'app-room-list',
  templateUrl: './room-list.component.html',
  styleUrls:["./room-list.less"]
})
export class RoomListComponent implements OnInit {
  @Input() set _param(value) {
    console.log(value);
    //单元
    if(value.level == 9){
      this.config.defaultParam = {
        houseName: value.houseName,
        order:'roomNumber',
        areaId:value.id,
      }
      this.getCount(value.id);
    }else{
      this.config.defaultParam = {
        houseName: value.houseName,
        order:'roomNumber',
        allParentId:value.id,
      }
      this.getCount(value.id);
    }
    
    this.parma = value;
    if (this.st)
    this.st.reload();
  };
  addFlag:boolean = this.aclService.canAbility("house-manage/add");//检查用户按钮权限
  editFlag:boolean =  this.aclService.canAbility("house-manage/edit");
  delFlag:boolean =  this.aclService.canAbility("house-manage/delete");
  watchFlag:boolean =  this.aclService.canAbility("house-manage/view");
  addPersonFlag:boolean =  this.aclService.canAbility("house-manage/adduser");
  parma: any;
  showDetail = true;
  activeData = null;
  @ViewChild('st', {static: false})st: TablePageComponent;
  communityCount:any=0;
  laneCount:any=0;
  houseCount:any=0;
  unitCount:any=0;
  landlordCount:any=0;
  personCount:any=0;
  cardCount:any=0;
  faceCount:any=0;
  roomCount:any=0
  config: SPage = {
    columns: [
      {
        index: 'id',
        title: '选中',
        type: ColumnType.checkbox
      },
      {
        index: 'roomName',
        title: '房间名',
        width:110,
        filter: {
          type: 'string'
        }
      },
      {
        index: 'roomNumber',
        title: '门牌号',
        width:110,
        filter: {
          type: 'string'
        }
      },
      {
        index:'roomPersonCount',
        title:'房间人数',
        width:80
      },
      {
        index: 'roomFloor',
        title: '所在楼层',
        width:100,
        filter: {
          type: 'string'
        }
      },
      {
        index: 'houseName',
        title: '所属区域',
        width:480,
        filter: {
          type: 'string'
        },
        //className:"w200"
      },
     /* {
        index: 'roomAcreage',
        title: '房间面积',

      },
      {
        index: 'roomDecoration',
        title: '装修描述',
      },
      {
        index: 'roomOrientation',
        title: '朝向描述',
      },*/
      {
        index: 'roomType',
        title: '类型',
        width:100,
        filter: {
          type: 'select',
          items: [
            {label: '出租屋', value:'1'},
            {label: '旅店', value:'2'},
            {label: '办公室', value: '3'},
            {label: '店面', value: '4'}
          ]
        }
      },
      {
        index: 'status',
        title: '状态',
        width:80,
        type: ColumnType.tag,
        tag: {
          false: {text:'停用', color: 'red'},
          true: {text:'启用', color: 'green'},
        },
        filter: {
          type: 'select',
          items: [
            {label: '启用', value: true},
            {label: '停用', value: false},
          ]
        }
      },
      /*{
        index: 'houseRemark',
        title: '备注'
      },*/
      {
        index: '',
        title: '操作',
        width:250,
        fixed:'right',
        buttons:[
          {
            text:'查看',
            iif: () => this.watchFlag,
            click:(_record)=>{
              this.detail(_record)
            }
          },
          {
            text:'编辑',
            iif: () => this.editFlag,
            click:(_record)=>{
              this.editDic(1 , _record)
            }
          },
          {
            text:'新增住户',
            iif: () => this.addPersonFlag,
            click:(_record)=>{
              this.addPerson(_record)
            }
          },
          {
            text:'导入人员',
            click:(_record)=>{
              this.importPersonFace(_record)
            }
          }
        ]
      },
    ],
    reqUrl: this.api.room.page.url,
    // editBtn: [
    //   {title: '查看'},
    //   {title: '编辑'},
    //   {title: '新增住户'}
    // ],
   // ps: 5,
  };

  selectedData: object[];
  editDic(type: 0 | 1 = 0, param?: object): void {
    console.log(param)
    if (type==0 && (!param['level']||param['level'] < 9)) {
      this.msg.warning("请选择单元后新增房间");
      return;
    }
    this.modal.create({
      nzTitle: type === 0 ? param['addType'] == 1? '批量添加房间' : '添加房间' : '修改房间',
      nzContent: RoomEditComponent,
      nzWidth: 1200,
      nzComponentParams: {
        type,
        param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
  }
  //点击导入人员
  importPerson(param:object):void{
    if (!param['level']||param['level'] < 8) {
      this.msg.warning("请选择楼栋或单元后导入人员");
      return;
    }
    const id = param['areaId']
    const areaRank = param['level']
    this.modal.create({
      nzTitle:"导入人员",
      nzContent: ImportPersonComponent,
      nzWidth: 600,
      nzComponentParams: {
        id,
        areaRank
      },
      nzOnOk: () => {
      },
      nzFooter: null
    })
  }

  importPersonFace(param):void{
    const id = param.id
    this.modal.create({
      nzTitle:"导入人员",
      nzContent: ImportPersonFaceComponent,
      nzWidth: 600,
      nzComponentParams: {
        id,
      },
      nzOnOk: () => {
      },
      nzFooter: null
    })
  }
  selectChange(e): void {
    this.selectedData = e;
  }
  operate(e): void {
    console.log(e);
    switch (e.title) {
      case '查看':
        this.detail(e.data)
        break;
      case '编辑':
        this.editDic(1 , e.data)
        break;
      case '新增住户':
        this.addPerson(e.data)
    }
  }
  addPerson(param) {
    const roomId=param.id;
    const roomNumber = param.roomNumber
    this.modal.create({
      nzTitle:  '新增住户',
      nzContent: HouseManagePersonAddComponent,
      nzWidth: "70%",
      nzComponentParams: {
        roomId,
        roomNumber
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
  }
  detail(param) {
    this.modal.create({
      nzTitle:  '房间详情',
      nzContent: RoomDetailComponent,
      nzWidth: 1200,
      nzComponentParams: {
        data: param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
  }
  deleteDic(): void {
    if (!this.selectedData || this.selectedData.length === 0) {
      this.msg.warning('请选择数据后操作!');
      return;
    }
    this.modal.confirm({
      nzTitle: '您确定要删除这些数据吗',
      nzOnOk: () => {
        const body = [];
        this.selectedData.forEach((item: any) => {
          body.push(item.id)
        })
        this.http.post(this.api.room.delete.url,body).subscribe((res:any) => {
          console.log(res);
          if (res.status === '200') {
            this.msg.success('删除成功');
            this.st.reload();
          }else{
            this.msg.error(res.message);
          }
        })

      }
    })
  }
  constructor(
    private modal: NzModalService,
    private msg: NzMessageService,
    private area: AreaService,
    private api: ApiService,
    private http: HttpClient,
    private changeDetectorRef:ChangeDetectorRef,
    private aclService: ACLService
  ) { }

  ngOnInit() {
  }

  dataProcess(data: STData[]){
    return data;
  }
  getCount(id:string){
    this.http.request<any>("post","/core/room/dataCount",{body:{'areaId':id}}).subscribe((res:any)=>{
      console.log(res);
       if(res.status == 200){
        let data=res.data;
        this.houseCount=data.houseCount;
        this.unitCount=data.hostCount;
        this.roomCount=data.roomCount;
        this.personCount=data.personCount;
        this.cardCount=data.cardCount;
      }
    })
  }
}
